library(tidyverse)
library(fixest)
library(showtext)
library(vtable)
library(writexl)
library(haven)
library(gridExtra)

setwd('D:\\WorkingPaper-Series\\Township_Light\\Replication_Files\\workdata')

#################################
########## Figure 6 #############
#################################

hetero <- read_delim('hetero_est.csv')
hetero <- hetero %>% mutate(upper_ci = coef+1.96*se, lower_ci = coef-1.96*se,quantile = as.factor(quantile))

hetero %>% ggplot(aes(type,coef,shape = quantile, color = quantile))+
  geom_hline(yintercept = 0,color = 'grey50',lty =2)+
  geom_point(aes(fill = quantile),position=position_dodge(0.3))+
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci),
                width = 0.2, lwd = .5,position=position_dodge(0.3))+
  theme_bw()+
  theme(legend.position = 'bottom',
        legend.background = element_rect(color = 'black', size = 0.1,linewidth = 0.1, fill = 'white'),
        legend.margin = margin(t = 0.05, r = 0.1, b = 0.05, l = 0.1, unit = "cm"),
        text = element_text(family = 'serif',size = 9),
        axis.text =  element_text(family = 'serif',size = 9))+
  labs(y = '', x = '')+
  ylim(-0.15,0.15)+
  scale_color_manual(values = c('1' = 'grey10', '2' = 'grey30','3' = 'grey50', '4' = 'grey70'), 
                     labels = c('Q1', 'Q2','Q3','Q4')) +
  scale_fill_manual(values = c('1' = 'grey10', '2' = 'grey30','3' = 'grey50', '4' = 'grey70'), 
                    labels = c('Q1', 'Q2','Q3','Q4')) +
  scale_shape_manual(values = c('1' = 21, '2' = 22,'3'=23,'4'=24), 
                     labels = c('Q1', 'Q2','Q3','Q4'))+
  guides(color = guide_legend(title = ""),shape = guide_legend(title = ""),fill = guide_legend(title = ""))+
  scale_x_discrete(labels = c('_county_dist'='Dist to County GOV','_county_gdp_pc'='County GDP (PC)',
                              '_fiscal_pressure'='Fiscal Pressure','_light10'='Initial Light','_population10'='Initial Population',
                              '_road13'='Initial Road','_topography'='Topography'))